<template>
	<view class="content">
		<u-navbar :fixed="false" title="修改工艺" :autoBack="true" :safeAreaInsetTop="true" leftIconColor="#fff">
		</u-navbar>

		<view class="MainBox">
			<u--form labelPosition="left" :model="formData" ref="uForm">

				<u-form-item labelWidth="120" label="工序名称" prop="ProcedureName" borderBottom leftIcon="file-text"
					required :leftIconStyle="{'font-size':'20px'}">
					<view class="x-f" style="justify-content: space-between;width: 100%;">
						<view @click="openBaseScreen('工序','radio')" style="flex: 1;">
							<u--input style="width: 100%;" v-model="formData.ProcedureName" disabled
								disabledColor="#ffffff" placeholder="请选择" border="none"></u--input>
						</view>

						<view class="x-f" slot="right">
							<u-icon name="arrow-right"></u-icon>

							<!-- <u-icon name="scan" size="26" style="margin-left: 8px;"></u-icon> -->
						</view>
					</view>
				</u-form-item>

				<u-form-item labelWidth="120" label="工作中心" prop="FWorkshopName" borderBottom leftIcon="share" required
					@click="openBaseScreen('工作中心','radio')" :leftIconStyle="{'font-size':'20px'}">
					<u--input v-model="formData.FWorkshopName" disabled disabledColor="#ffffff" placeholder="请选择"
						border="none"></u--input>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>
				<u-form-item labelWidth="120" label="是否外协" prop="OutsourceName" borderBottom leftIcon="file-text"
					required @click="openBaseScreen('是否外协','radio')" :leftIconStyle="{'font-size':'20px'}">
					<u--input v-model="formData.OutsourceName" disabled disabledColor="#ffffff" placeholder="请选择"
						border="none"></u--input>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>

				<u-form-item labelWidth="120" label="设备类型" prop="EquipmentTypeName" borderBottom leftIcon="file-text"
					@click="openBaseScreen('设备类型','radio')" :leftIconStyle="{'font-size':'20px'}">
					<u--input v-model="formData.EquipmentTypeName" disabled disabledColor="#ffffff" placeholder="请选择"
						border="none"></u--input>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>

				<u-form-item labelWidth="120" label="模具类型" prop="MoludTypeName" borderBottom leftIcon="file-text"
					@click="openBaseScreen('模具类型','radio')" :leftIconStyle="{'font-size':'20px'}">
					<u--input v-model="formData.MoludTypeName" disabled disabledColor="#ffffff" placeholder="请选择"
						border="none"></u--input>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>

				<u-form-item labelWidth="120" label="工价" prop="LabourCost" borderBottom leftIcon="coupon"
					:leftIconStyle="{'font-size':'20px'}">
					<u--input v-model="formData.LabourCost" placeholder="请输入" border="none"></u--input>
				</u-form-item>
				<u-form-item labelWidth="120" label="工时" prop="ManHour" borderBottom leftIcon="coupon"
					:leftIconStyle="{'font-size':'20px'}">
					<u--input v-model="formData.ManHour" placeholder="请输入" border="none"></u--input>
				</u-form-item>
				<u-form-item labelWidth="120" label="小时工价" prop="HourGJ" leftIcon="coupon"
					:leftIconStyle="{'font-size':'20px'}">
					<u--input v-model="formData.HourGJ" placeholder="请输入" border="none"></u--input>
				</u-form-item>


			</u--form>

		</view>


		<view class="BottomBox" :style="$utils.disposeSafeBottomFun()">
			<button class="SubBtn" style="width: 90%;" @click="SubmitBtn">确定</button>
		</view>


	</view>
</template>

<script>
	// 工艺路线
	export default {
		data() {
			return {
				formData: {
					ProcedureName: '',
					ProcedureID: '',
					ProcedureCode: '',
					FWorkshopID: '',
					FWorkshopName: '',
				},
				FWorkshop: {}
			}
		},
		onLoad(option) {
			var that = this;
			let BaseData = this.$store.state.BaseScreenParm;
			// console.log(JSON.stringify(BaseData))

			// 主表车间
			this.FWorkshop = BaseData.FWorkshop;

			let data = that.InitData(BaseData);
			// let data = that.InitData(BaseData.data);

			that.formData = Object.assign(that.formData, data);
			// console.log(JSON.stringify(that.formData))


		},
		mounted() {
			let that = this;
			uni.$on('EditInfo_Choice', function(ret) {
				// console.log(ret)
				if (ret.type == '工序') {
					let data = that.InitData(ret);
					that.formData = data;
					that.formData = Object.assign(that.formData, data);
				} else if (ret.type == '工作中心') {
					let data = ret.data;
					that.formData = Object.assign(that.formData, data);
					that.formData = JSON.parse(JSON.stringify(that.formData))
				} else if (ret.type == '是否外协') {
					let data = ret.data;
					that.formData = Object.assign(that.formData, data);
					that.formData = JSON.parse(JSON.stringify(that.formData))
				} else if (ret.type == '设备类型') {
					let data = ret.data;
					that.formData = Object.assign(that.formData, data);
					that.formData = JSON.parse(JSON.stringify(that.formData))
				} else if (ret.type == '模具类型') {
					let data = ret.data;
					that.formData = Object.assign(that.formData, data);
					that.formData = JSON.parse(JSON.stringify(that.formData))
				}
			})
		},
		onUnload() {
			uni.$off('EditInfo_Choice');
		},
		methods: {
			InitData(obj) {
				let data = obj.data;
				let FWorkshop = obj.FWorkshop;
				console.log(data);
				// 判断是否有工作中心，或工作中心数据是否为空，如果是空，则使用主表的工作中心数据
				if (!data.hasOwnProperty('FWorkshopID') || this.$utils.isEmpty(data['FWorkshopName'])) {
					data.FWorkshopName = FWorkshop['FWorkshopName'] || ''; // 车间 - 默认获取从最外层带进来的车间信息
					data.FWorkshopID = FWorkshop['FWorkshopID'] || 0;
				}

				// 判断是否有外协字段
				if (!data.hasOwnProperty('Outsource')) {
					data.Outsource = 0; // 是否外协
					data.OutsourceName = '否'; // 是否外协
				} else {
					data.OutsourceName = data.Outsource == 1 ? '是' : '否';
				}
				if (!data.hasOwnProperty('EquipmentTypeID')) {
					data.EquipmentTypeID = 0; // 设备类型
					data.EquipmentTypeName = ''; // 设备类型

				}
				if (!data.hasOwnProperty('MoludTypeID')) {
					data.MoludTypeID = 0; // 模具类型
					data.MoludTypeName = ''; // 模具类型
				}
				if (!data.hasOwnProperty('LabourCost')) {
					data.LabourCost = 0; // 工价
				}
				if (!data.hasOwnProperty('ManHour')) {
					data.ManHour = 0; // 工时
				}
				if (!data.hasOwnProperty('HourGJ')) {
					data.HourGJ = 0; // 工时工价
				}
				return data;
			},
			openBaseScreen(str, type) { // 前往筛选页
				let that = this;
				if (type == 'radio') {
					let obj = null;
					let url = '/commonPage/Tool/baseScreen';
					if (str == '工序') {
						obj = {
							allowClear: false,
							cmd: 'XGD_Procedure',
							type: str,
							ListeningName: 'EditInfo_Choice',
							// multiple: false,
							serverSearch: {
								isPaging: true, //是否分页
								searchKey: '', //搜索关键词，应包含在param中
								showText: 'ProcedureName', //查询结果显示的字段
								pageIndexStr: 'PageIndex', //分页页码字段，应包含在param中
								// subText: [{
								// 	label: '描述',
								// 	labelKey: 'desc'
								// }],
								ChoiceDataKey: 'ProcedureID',
								ChoiceDataData: this.formData.ProcedureID,
							},
							param: {
								SearchKey: '',
								PageIndex: 1,
								PageSize: 30
							}
						}

					} else if (str == '工作中心') {
						obj = {
							allowClear: false,
							cmd: 'XGD_Workshop',
							type: str,
							ListeningName: 'EditInfo_Choice',
							// multiple: false,
							serverSearch: {
								isPaging: true, //是否分页
								searchKey: '', //搜索关键词，应包含在param中
								showText: 'FWorkshopName', //查询结果显示的字段
								pageIndexStr: 'PageIndex', //分页页码字段，应包含在param中
								// subText: [{
								// 	label: '描述',
								// 	labelKey: 'desc'
								// }],
								ChoiceDataKey: 'FWorkshopID',
								ChoiceDataData: this.formData.FWorkshopID,
							},
							param: {
								SearchKey: '',
								PageIndex: 1,
								PageSize: 30
							}
						}
					} else if (str == '是否外协') {

						obj = {
							allowClear: false,
							cmd: '',
							type: str,
							ListeningName: 'EditInfo_Choice',
							// multiple: false,
							serverSearch: {
								isPaging: true, //是否分页
								searchKey: '', //搜索关键词，应包含在param中
								showText: 'OutsourceName', //查询结果显示的字段
								// pageIndexStr: 'pageIndex', //分页页码字段，应包含在param中
								// subText: [{
								// 	label: '描述',
								// 	labelKey: 'desc'
								// }],
								ChoiceDataKey: 'Outsource',
								ChoiceDataData: this.formData.Outsource,
							},
							param: {
								data: [{
									OutsourceName: '是',
									Outsource: 1
								}, {
									OutsourceName: '否',
									Outsource: 0
								}]
							}
						};

						url = '/commonPage/Tool/baseScreen?select=1';
					} else if (str == '设备类型') {
						obj = {
							allowClear: false,
							cmd: 'XGD_EquipmentType',
							type: str,
							ListeningName: 'EditInfo_Choice',
							// multiple: false,
							serverSearch: {
								isPaging: true, //是否分页
								searchKey: '', //搜索关键词，应包含在param中
								showText: 'EquipmentTypeName', //查询结果显示的字段
								pageIndexStr: 'PageIndex', //分页页码字段，应包含在param中
								// subText: [{
								// 	label: '描述',
								// 	labelKey: 'desc'
								// }],
								ChoiceDataKey: 'EquipmentTypeID',
								ChoiceDataData: this.formData.EquipmentTypeID,
							},
							param: {
								SearchKey: '',
								PageIndex: 1,
								PageSize: 30
							}
						}
					} else if (str == '模具类型') {
						obj = {
							allowClear: false,
							cmd: 'XGD_MoludType',
							type: str,
							ListeningName: 'EditInfo_Choice',
							// multiple: false,
							serverSearch: {
								isPaging: true, //是否分页
								searchKey: '', //搜索关键词，应包含在param中
								showText: 'MoludTypeName', //查询结果显示的字段
								pageIndexStr: 'PageIndex', //分页页码字段，应包含在param中
								// subText: [{
								// 	label: '描述',
								// 	labelKey: 'desc'
								// }],
								ChoiceDataKey: 'MoludTypeID',
								ChoiceDataData: this.formData.MoludTypeID,
							},
							param: {
								SearchKey: '',
								PageIndex: 1,
								PageSize: 30
							}
						}
					}

					this.$store.commit('ChangeAndSetStateData', {
						Name1: 'BaseScreenParm',
						data: obj
					})

					uni.navigateTo({
						url: url
					})

				} else if (type == 'other') {

				}

			},
			SubmitBtn() { // 确定按钮
				let that = this;

				uni.$emit('EditInfo_Change', {
					type: '工序',
					msg: '',
					data: that.formData
				});

				uni.navigateBack();
			},
		}
	}
</script>

<style lang="scss">
	.content {
		display: flex;
		flex-direction: column;
		height: 100vh;
		// height: calc(100vh - 60px);
		background-color: #f9f9f9;

		.MainBox {
			flex: 1;
			overflow: auto;

			// 表单1
			.u-form {
				// padding: 0px 10px;

				.u-form-item {
					background-color: #fff;
					padding: 0 16px;
				}
			}
		}

		.BottomBox {
			display: flex;
			justify-content: space-evenly;
			align-items: center;
			padding: 10px 15px;
			background-color: #fff;
			border-top: 1px solid #efefef;

			.SaveBtn {
				width: 40%;
				background-color: #fff;
				color: #51d0d0;
				border: 1px solid #51d0d0;
				border-radius: 12px;
			}

			.SubBtn {
				width: 40%;
				background-color: #51d0d0;
				color: #fff;
				border-radius: 12px;
			}
		}

	}
</style>